//
//  VRAlarmViewController.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/9.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRAlarmViewController.h"
#import "VRHistoryTaskViewController.h"

#import "VRContactCell.h"

#import "VRContactModel.h"

@interface VRAlarmViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray<VRContactModel *> *contactArray;

@end

@implementation VRAlarmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = ScrollViewBgColor;
    self.title = @"报警";
    
    [self setupSubView];
    
    weakify(self)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        strongify(self)
        [self getContact];
    }];
    
    [self getContact];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess) name:kLoginSuccess object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logout) name:kLogout object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setupSubView
{
    [self.view addSubview:self.tableView];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 42)];
    headerView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableHeaderView = headerView;
    
    UIImageView *iconImgView = [[UIImageView alloc] init];
    iconImgView.image = [UIImage imageNamed:@"contact"];
    iconImgView.contentMode = UIViewContentModeScaleAspectFit;
    iconImgView.frame = CGRectMake(20, 0, 18, 18);
    iconImgView.centerY = 21;
    [headerView addSubview:iconImgView];
    
    UILabel *nameLbl = [[UILabel alloc] init];
    nameLbl.text = @"我的紧急联系人";
    nameLbl.textColor = RGB(237, 11, 32);
    nameLbl.font = VRFont(18);
    nameLbl.frame = CGRectMake(CGRectGetMaxX(iconImgView.frame) + 4, 0, 150, 42);
    [headerView addSubview:nameLbl];
    
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 56)];
    self.tableView.tableFooterView = footerView;
    
    XButton *viewMonitorBtn = [XButton buttonWithType:UIButtonTypeCustom];
    //    goCardPackageBtn.layer.cornerRadius = 2;
    //    goCardPackageBtn.layer.borderColor = RGB(248, 177, 90).CGColor;
    viewMonitorBtn.layer.borderColor = MAINColor.CGColor;
    viewMonitorBtn.layer.borderWidth = 1.0;
    [viewMonitorBtn setTitle:@"查看历史任务" forState:UIControlStateNormal];
    viewMonitorBtn.titleLabel.font = VRFont(14);
    [viewMonitorBtn setTitleColor:MAINColor forState:UIControlStateNormal];
    [viewMonitorBtn setTitleColor:RGB(211, 209, 209) forState:UIControlStateDisabled];
    [viewMonitorBtn addTarget:self action:@selector(onHistoryBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:viewMonitorBtn];
    [viewMonitorBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(footerView);
        make.centerX.mas_equalTo(footerView);
        make.width.mas_equalTo(108);
        make.height.mas_equalTo(32);
    }];
}


#pragma mark - networking
- (void)getContact
{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"UserID"] = [VRUser shareUser].token;
    //    param[@"fIsAreaManage"] = @([VRUser shareUser].fIsAreaManage);
    
    weakify(self)
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    hud.bezelView.backgroundColor = RGBA(0, 0, 0, 0.9);
//    hud.bezelView.style = MBProgressHUDBackgroundStyleBlur;
    hud.mode = MBProgressHUDModeIndeterminate;
    [JKNetworking postWithSign:GetContact parameters:param success:^(NSDictionary *responseObject) {
        
        strongify(self)
        NSLog(@"responseObject:%@", responseObject);
        NSInteger code = [responseObject[@"state"] integerValue];
        NSString *msg = responseObject[@"message"];
        
        if (code == 0) {
            
            self.contactArray = [VRContactModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.tableView reloadData];
                [hud hideAnimated:YES];
            });
        }else if(msg.length > 0){
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showErrorWithStatus:msg];
                [hud hideAnimated:YES];
            });
        }
        [self.tableView.mj_header endRefreshing];
    } error:^(NSError * _Nonnull error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:@"未知错误"];
            [hud hideAnimated:YES];
            [self.tableView.mj_header endRefreshing];
        });
    }];
}

#pragma mark - action
- (void)onHistoryBtnClick:(UIButton *)sender
{
    VRHistoryTaskViewController *historyVc = [[VRHistoryTaskViewController alloc] init];
    [self.navigationController pushViewController:historyVc animated:YES];
}

- (void)loginSuccess
{
    
    [self getContact];
}

- (void)logout
{
    
}



#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.contactArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 92;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    VRContactCell *cell = [VRContactCell cellWith:tableView];
    cell.model = self.contactArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - lazy
- (UITableView *)tableView
{
    if (_tableView == nil) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = ScrollViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = [[UIView alloc] init];
        
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];
        _tableView.tableFooterView = view1;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.sectionFooterHeight = 0.01;
        [self.view addSubview:self.tableView];
        //        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.left.right.bottom.mas_equalTo(self.view);
        //            make.top.mas_equalTo(0);
        //        }];
    }
    return _tableView;
}


@end
